package com.mall.service;

import java.util.List;

import com.mall.ibatis.bean.FunMethod;
import com.mall.ibatis.bean.FunctionMethod;
import com.mall.ibatis.bean.ICell;
import com.mall.ibatis.bean.JQGrid;
import com.mall.ibatis.bean.Method;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.ibatis.persistence.DaoConfig;
import com.mall.ibatis.persistence.iface.FunctionMethodDAO;
import com.ibatis.dao.client.DaoManager;

public class FunctionMethodService {
	private static final FunctionMethodService instance = new FunctionMethodService();
	
	private DaoManager daoManager = DaoConfig.getDaomanager();
	
	private FunctionMethodDAO functionMethodDAO = null;
	
	private FunctionMethodService(){
		functionMethodDAO  = (FunctionMethodDAO) daoManager.getDao(FunctionMethodDAO.class);
	}
	
	public static FunctionMethodService getInstance(){
		return instance;
	}
	
	//查询方法method表中有哪些方法，并加载到页面的选择框中
	public List<Method> selectAllMethod() { 
		return functionMethodDAO.selectAllMethod();
	}

	//功能方法关系列表显示
	public JQGrid selectAllFunctionMethod() {
		List<FunctionMethod> list = functionMethodDAO.selectAllFunctionMethod();
		List<FunMethod> fms = functionMethodDAO.selectAllFunMethod();
		//往list集合里面的methods属性添加所有方法
		for(FunctionMethod fm : list){
			String temp = "";
			for(FunMethod fun : fms){
				if(fm.getFunctionMethodId().equals(fun.getFunId())){
					temp = temp + fun.getMethodName() + ",";
				}
			}
			if("".equals(temp)){
				fm.setMethods(temp);
			}else{
				temp = temp.substring(0,temp.length() - 1);
				fm.setMethods(temp);
			}
		}
		JQGrid jqGrid = new JQGrid();
		
		jqGrid.setPage(1);
		jqGrid.setRecord(list.size());
		jqGrid.setTotal(1);
		
		List<ICell> rowsList = jqGrid.getRowXML();
		
		
		for(FunctionMethod functionMethod :list){
			rowsList.add(functionMethod);
		}
		return jqGrid;
	}
	
	//把方法和功能多对一得关系保存在fun_method表中
	public ReturnMessage updateFunMethod(String id, String methodId) {
		ReturnMessage message = null;
		String[] str = null;
		if(methodId != null && methodId.trim().length() > 0){
			str = methodId.split(",");				
		}
		
		daoManager.startTransaction();
		try{
			functionMethodDAO.deleteFunMethod(id);
			if(str != null){
				for(int i=0;i<str.length;i++){
					functionMethodDAO.updateFunMethod(id,str[i]);
				}
			}
			message = new ReturnMessage("1");
			daoManager.commitTransaction();
		}catch(Exception e){
			message = new ReturnMessage("0","保存失败" + e.getMessage());
		}finally{
			daoManager.endTransaction();
		}
		return message;
	}

	//查询已经勾选过的方法
	public String methodIsChoose(String funId) {
		List<String> list = functionMethodDAO.methodIsChoose(funId);
		StringBuffer bf = new StringBuffer();
		if(list.size() > 0){
			bf.append("<str>");
			for(String s : list){
				bf.append(s + ",");
			}
			bf.append("</str>");
			return bf.toString();
		}else{
			return bf.append("<str>notData</str>").toString();
		}
	}
}
